package org.gis.mapserver.source;

import java.util.Map;
import org.gis.utils.activator.Activator;
import org.gis.utils.beanloader.ConfigBeanLoader;

/**
 *
 * @author gus
 */
public class SourceFactory
{
    private Map<String, ? extends SourceBuilder> sourceMapping;
    private ConfigBeanLoader configBeanLoader;

    public ConfigBeanLoader getConfigBeanLoader()
    {
        return configBeanLoader;
    }

    public void setConfigBeanLoader( ConfigBeanLoader configBeanLoader )
    {
        this.configBeanLoader = configBeanLoader;
    }

    public void setSourceMapping( Map<String, ? extends SourceBuilder> sourceMapping )
    {
        this.sourceMapping = sourceMapping;
    }

    public Source makeSource( String sourceName ) throws Exception
    {
        if ( sourceName == null || sourceName.isEmpty() )
        {
            throw new IllegalArgumentException("sourceName");
        }
        SourceBuilder builder = getBuilderTypeName(sourceName);
        if ( builder == null )
        {
            throw new Exception(String.format("can not create builder for source %s", sourceName));
        }
        return builder.makeSource();
    }

    private SourceBuilder getBuilderTypeName( String sourceName ) throws Exception
    {
        return sourceMapping.get(sourceName);
    }
}
